package com.junlaninfo.service;

import com.junlaninfo.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by 辉 on 2020/7/24.
 * 同步一个线程执行，这会造成任务的执行效率低下
 */
@RestController
public class LoginService1 {
    private static final Logger log = LoggerFactory.getLogger(LoginService1.class);

    @GetMapping("/login1")
    public String login(UserEntity userEntity) throws ExecutionException, InterruptedException {
        //1、先查询登录是否成功
        UserEntity userEntity1 = dbLogin(userEntity);
        if (userEntity1==null){
            return "登录名或者密码不对";
        }
        //2、写日志
        loginLog( userEntity);
        //3、发短信
        sendSms( userEntity);
        //4、发邮件
        sendEmail( userEntity);
        return  "登录成功";
    }

    private UserEntity dbLogin(UserEntity userEntity) {
        log.info(">>>1.正在查询数据库登陆<<<");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
        return userEntity;
    }

    private void loginLog(UserEntity userEntity) {
        log.info(">>>2.异步开始写登陆的日志<<<");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
//        int i = 1 / 0;
    }

    private void sendSms(UserEntity userEntity) {
        log.info(">>>3.异步开始发送短信<<<");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
    }

    private void sendEmail(UserEntity userEntity) {
        log.info(">>>4.异步开始发送邮件<<<");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
    }
}
